package org.qphoto;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class NativeDBHelper extends SQLiteOpenHelper {

	private final static String DATABASE_NAME = "smartfacedb2";
	private final static int DATABASE_VERSION = 1;
	
	private final static String sqlCreateTableUserInfo = 
			"CREATE TABLE tbUserInfo (" + 
			"userid INTEGER PRIMARY KEY ASC," +  
			"username TEXT," +                   
			"realname TEXT," +                  
			"password TEXT DEFAULT NULL," +    
			"qqnumber TEXT DEFAULT NULL," +   
			"phone TEXT DEFAULT NULL,"     +    
			"avatar TEXT DEFAULT NULL"    +    
			");";
	
	private final static String sqlCreateTableMessages = 
			"CREATE TABLE tbMessages (" + 
			"messageid INTEGER PRIMARY KEY ASC," +  
			"userid INTEGER NOT NULL," +           
			"posttime TEXT NOT NULL," +             
			"breply INTEGER DEFAULT '0'," +          
			"rootmessageid INTEGER DEFAULT '0'," +  
			"content  TEXT DEFAULT NULL," +      
			"imageuri TEXT DEFAULT NULL," +        
			"audiouri TEXT DEFAULT NULL," +       
			"videouri TEXT DEFAULT NULL" +         
			");";
	
	private final static String sqlCreateTableFriends = 
			"CREATE TABLE tbFriends (" + 
			"userid1 INTEGER NOT NULL," +           // �û�1 
			"userid2 INTEGER NOT NULL"  +           // �û�2
			");";
	
	public NativeDBHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub			
		db.execSQL(sqlCreateTableUserInfo); 
		db.execSQL(sqlCreateTableMessages); 
		db.execSQL(sqlCreateTableFriends); 
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub
		String sqlDropTableUserInfo   = "DROP TABLE tbUserInfo;";
		String sqlDropTabletbMessages = "DROP TABLE tbMessages;";
		String sqlDropTabletbFriends  = "DROP TABLE tbFriends;";
		
		db.execSQL(sqlDropTableUserInfo);
		db.execSQL(sqlDropTabletbMessages);
		db.execSQL(sqlDropTabletbFriends);
		
		db.execSQL(sqlCreateTableUserInfo); 
		db.execSQL(sqlCreateTableMessages); 
		db.execSQL(sqlCreateTableFriends); 
	}
	
	public void Insert(String sql)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);
	}
	
	public void Update(String sql)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);
	}
	
	public void Delete(String sql)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);
	}
	
	public Cursor Select(String sql)
	{
		SQLiteDatabase db = this.getReadableDatabase();
	    Cursor cursor     = db.rawQuery(sql, null);
	    return cursor;
	}

}
